package com.st.server;

import com.st.reqandresp.Request;
import com.st.reqandresp.Response;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangshuai on 2017/8/27.
 */
public class Server {

    private ServerSocket serverSocket;

    public Server(){}

    public void start(int port){
        try{
            serverSocket = new ServerSocket(port);
            this.receive();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void receive(){
        try{
            Socket client = serverSocket.accept();
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            InputStream inputStream = client.getInputStream();
            Request request = new Request(inputStream);
            request.setCharacterEncoding("UTF-8");
            Map<String,List<String>> map = request.getParameterMap();
            for(Map.Entry<String,List<String>> entry : map.entrySet()){
                System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
            }
            Response response = new Response(client.getOutputStream());
            response.print("231423");
            response.sendToClient();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void close(){}
}
